/**
 * This file is loaded via the <script> tag in the index.html file and will
 * be executed in the renderer process for that window. No Node.js APIs are
 * available in this process because `nodeIntegration` is turned off and
 * `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
 * to expose Node.js functionality from the main process.
 */
const { ipcRenderer } = require('electron');

window.onload = () => {
	var input = this.document.querySelector("#input");
	var input_btn = this.document.querySelector("#input_btn");
	var clear_btn = this.document.querySelector("#clear_btn");
	var myBody = this.document.querySelector("#myBody");
    var responseElement = document.getElementById('response');
	var call_api_btn = document.getElementById('call_api_btn');
    var get_db_btn = document.getElementById('get_db_btn');
	var getDbReturnElement = document.getElementById('get_db_return');

	input_btn.onclick = () => {
		myBody.innerHTML = input.value;
	}
	clear_btn.onclick = () => {
		myBody.innerHTML = "";
        responseElement.innerHTML = "";
		getDbReturnElement.innerHTML = "";
	}
    call_api_btn.onclick = () => {
        ipcRenderer.on('fetch-data-response', (event, data) => {
           responseElement.innerHTML = JSON.stringify(data);
        });

        ipcRenderer.send('fetch-data');
    }
	
	get_db_btn.onclick = () => {
        ipcRenderer.on('get-db-response', (event, data) => {
			getDbReturnElement.innerHTML = data;
        });

        ipcRenderer.send('get-db');
    }
}
